package ru.compscicenter.databases.zaikina.common.commands;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import ru.compscicenter.databases.zaikina.common.commands.bodies.WordsBody;
import ru.compscicenter.databases.zaikina.common.model.Word;

import java.util.ArrayList;

public class WordsCommand extends SysCommand<ArrayList<Word>, WordsBody> {
    public WordsCommand(Integer type, WordsBody body) {
        super(type, body, -1);
    }

    public WordsCommand(Integer type, ArrayList<Word> body) {
        super(type, body, -1);
    }

    public static WordsCommand parse(String jsonStr) {
        JSONObject obj = (JSONObject) JSONValue.parse(jsonStr);
        if (SysCommandType.isWordsCommand(SysCommand.parseType(obj))) {
            Integer type = Integer.parseInt(obj.get("type").toString());
            WordsBody body = new WordsBody();
            body.getFromJsonString(obj.get("body").toString());
            return new WordsCommand(type, body);
        }
        return null;
    }
}
